package tree;


import tree.data.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 *
 * 路径总和
 *
 * https://leetcode.cn/problems/path-sum/?envType=study-plan-v2&envId=top-interview-150
 *
 */
public class HasPathSum<T> {

    public boolean hasPathSum(TreeNode root, int targetSum) {

        if (root==null){
            return false;
        }

        Queue<TreeNode> nodeQueue=new ArrayDeque<>();
        Queue<Integer> valQueue=new ArrayDeque<>();

        nodeQueue.add(root);
        valQueue.add(root.val);

        while (!nodeQueue.isEmpty()){
            TreeNode node=nodeQueue.poll();
            int temp=valQueue.poll();

            if (node.left==null&&node.right==null){
                if (temp==targetSum){
                    return true;
                }
                continue;
            }


            if (node.left!=null){
                nodeQueue.add(node.left);
                valQueue.add(node.left.val+temp);
            }

            if (node.right!=null){
                nodeQueue.add(node.right);
                valQueue.add(node.right.val+temp);
            }
        }

        return false;

    }



}
